package com.cs2340.taskplusplus;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ViewSwitcher;

/**
 * The main activity for the program, it presents the login screen
 * and processes attempted logins as well as requests to create new users.
 * 
 * @author GRILLENBANNER
 *
 */
public class TaskMain extends Activity implements OnClickListener {
	/**
	 * These variables are the flags for different dialog messages
	 */
	private static final int DIALOG_NO_ACCOUNT_ID = 0;
	private static final int DIALOG_BAD_PASS_ID = 1;
	private static final int DIALOG_LOGIN_ID = 2;
	private static final int DIALOG_ACCOUNT_EXISTS_ID = 3;
	private static final int DIALOG_FIELD_BLANK_ID = 4;
	private static final int DIALOG_NEW_ACCOUNT_ID = 5;
	/**
	 * This variable lets us switch between the login view and the new account view
	 */
	private ViewSwitcher vs;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AccountDatabase.buildDatabase(this);

        setContentView(R.layout.login);
        vs = (ViewSwitcher) findViewById(R.id.viewSwitcher);

    	View loginButton = findViewById(R.id.loginButton);
        loginButton.setOnClickListener(this);
        View registerB = findViewById(R.id.registerButton);
        registerB.setOnClickListener(this);
        
        View createUser = findViewById(R.id.createUser);
        createUser.setOnClickListener(this);
        View registerBack = findViewById(R.id.regBackButton);
        registerBack.setOnClickListener(this);
    }
    
    /**
     * Detects when the user clicks a button and reacts appropriately
     * 
     * @param View v The view that was clicked
     */
    public void onClick(View v)
    {    	
    	switch(v.getId())
    	{
    	case (R.id.loginButton):
    		String name = ((EditText)findViewById(R.id.usernameText)).getText().toString();
    		String pass = ((EditText)findViewById(R.id.passwordText)).getText().toString();
    				
    		// Account not found
    		if (!AccountDatabase.checkAccountExists(name))
    		{
    			showDialog(DIALOG_NO_ACCOUNT_ID);
    		}
    		//found
    		else{
    			//correct pass
    			if(AccountDatabase.checkCorrectPassword(name, pass))
    			{
    				new Account(name);
    				//showDialog(DIALOG_LOGIN_ID);
    				//now actually log in
    				Intent intent = new Intent(this, TaskListScreen.class);
    				intent.putExtra("Task List Name", Account.username+"_MasterList");
    				finishActivity(0);
    				startActivity(intent);
    			}
    			//incorrect pass
    			else{
    				showDialog(DIALOG_BAD_PASS_ID);
    			}
    		}
    		
    		
    		break;
    		
    	case (R.id.regBackButton):
    		vs.showPrevious();
    		break;

    	case (R.id.registerButton):
    		vs.showNext();
    		break;
    		
    		
    	case (R.id.createUser):
    		String regName = ((EditText)findViewById(R.id.regUser)).getText().toString();
    		String regPass = ((EditText)findViewById(R.id.regPass)).getText().toString();
    		String regEmail = ((EditText)findViewById(R.id.regEmail)).getText().toString();
    		String regRname = ((EditText)findViewById(R.id.regRealname)).getText().toString();
    		    		
            //if account exists
            if (AccountDatabase.checkAccountExists(regName))
    		{
    			showDialog(DIALOG_ACCOUNT_EXISTS_ID);
    		}
    		//if any field blank
    		else if ((regName.length()==0)||(regPass.length()==0))
    		{
    			showDialog(DIALOG_FIELD_BLANK_ID);
    		}
    		//else
    		else
    		{
    			new Account(regName, regPass, regEmail, regRname);
        		vs.showPrevious();
        		showDialog(DIALOG_NEW_ACCOUNT_ID);
    		}
    		//Account acc = new Account(regName, regPass, regRname, regEmail);
    		//add it to table or whatever AJ
    		//as it is, DO NOT actually login, this will take you back to login screen
    		break;
    	}
    }	
    
    /**
     * Ends the activity correctly when the user exits
     */
    public void onStop()
    {
    	super.onStop();
    	this.finish();
    }
    
    /**
     * Generates the correct dialog message, based on its id
     * 
     * @param int id The identifying number of the desired dialog message
     */
    protected Dialog onCreateDialog(int id) {
    	String message = "GUYS WHAT IS THIS MESSAGE I DON'T EVEN"; //default message
        switch(id) {
        case DIALOG_NO_ACCOUNT_ID:
        	message = "ACCOUNT NOT FOUND";
            break;
        case DIALOG_LOGIN_ID:
        	message = "LOGIN SUCCESSFUL";
            break;
        case DIALOG_BAD_PASS_ID:
        	message = "WRONG PASSWORD";
            break;
        case DIALOG_ACCOUNT_EXISTS_ID:
        	message = "That account name is already in use. Please choose another.";
            break;
        case DIALOG_FIELD_BLANK_ID:
        	message = "Please do not leave any fields blank.";
            break;
        case DIALOG_NEW_ACCOUNT_ID:
        	message = "NEW ACCOUNT SUCCESSFULLY MADE";
            break;
        }
        
        Dialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(message)
	       .setCancelable(true)
	       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface dialog, int id) {
	                dialog.cancel();
	           }
	       });
        AlertDialog alert = builder.create();
    	dialog = alert;
        return dialog;
    }
    
    
}